//
//  ScrollViewController.m
//  UI豆瓣2015到2016跨年作业
//
//  Created by LHY on 15/12/31.
//  Copyright © 2015年 Lanou 32 李昊宇. All rights reserved.
//

#import "ScrollViewController.h"
#import "UIImageView+WebCache.h"
#import "ActivityModel.h"
#import "MovieDataBaseHandle.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ScrollViewController ()


@property (nonatomic , retain)UIScrollView *scrollView;


@property (nonatomic , retain)NSDictionary *dic;
@end

@implementation ScrollViewController

- (void)dealloc
{
    [_str release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
      self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.5 alpha:1];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
   _scrollView.contentSize =  CGSizeMake(0, HEIGHT * 10);
    [self.view addSubview:_scrollView];
    [_scrollView release];
     
    self.navigationItem.title = self.str;

    
    
//    
    // 取出当前字典
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    self.dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];


    NSDictionary *dicNow = _dic[@"events"][self.indexPath.row];
    
    // 标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];
    title.text = dicNow[@"title"];
    title.font = [UIFont systemFontOfSize:22];
    [_scrollView addSubview:title];
    [title release];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, 120, 180)];
    [image sd_setImageWithURL:[NSURL URLWithString:dicNow[@"image"]]];
    [_scrollView addSubview:image];
    [image release];

    // 时间
    UIImageView *imageTime = [[UIImageView alloc] initWithFrame:CGRectMake(150, 70, 30, 30)];
    imageTime.image = [[UIImage imageNamed:@"icon_date@2x.png"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    [_scrollView addSubview:imageTime];
    
    UILabel *time = [[UILabel alloc] initWithFrame:CGRectMake(180, 70, 200, 30)];
    NSString *str1 = dicNow[@"begin_time"];
    NSString *str2 = dicNow[@"end_time"];
    time.text = [NSString stringWithFormat:@"%@ -- %@", [str1 substringWithRange:NSMakeRange(5, 11)], [str2 substringWithRange:NSMakeRange(5, 11)]];
    [_scrollView addSubview:time];
    
    
    // 所有者
    UIImageView *imageName = [[UIImageView alloc] initWithFrame:CGRectMake(150, 110, 30, 30)];
    imageName.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    [_scrollView addSubview:imageName];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(180, 110, 200, 30)];
    name.text = dicNow[@"owner"][@"name"];
    [_scrollView addSubview:name];
    
    
    // 类型
    UIImageView *category_name_Image = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 30, 30)];
    category_name_Image.image = [UIImage imageNamed:@"icon_catalog@2x.png"];
    [_scrollView addSubview:category_name_Image];
    
    UILabel *category_name = [[UILabel alloc] initWithFrame:CGRectMake(180, 150, 200, 30)];
    category_name.text = [NSString stringWithFormat:@"类型：%@", dicNow[@"category_name"]];
    [_scrollView addSubview:category_name];
    
    
    // 地址
    UIImageView *imageAddress = [[UIImageView alloc] initWithFrame:CGRectMake(150, 190, 30, 30)];
    imageAddress.image = [UIImage imageNamed:@"icon_spot@2x.png"];
    [_scrollView addSubview:imageAddress];
    
    UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(180, 190, 180, 60)];
    address.text = dicNow[@"address"];
    address.numberOfLines = 0;
    address.lineBreakMode = NSLineBreakByClipping;
    [_scrollView addSubview:address];
    
    //
    UILabel *myTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 270, 100, 30)];
    myTitle.text = @"活动介绍";
    myTitle.font = [UIFont boldSystemFontOfSize:22];
    [_scrollView addSubview:myTitle];
    
    //
    UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(20, 320, WIDTH - 40, HEIGHT)];
    content.text = dicNow[@"content"];
    content.numberOfLines = 0;
    [_scrollView addSubview:content];
    [content release];
    
    
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAction) target:self action:@selector(bar:)];
    self.navigationItem.rightBarButtonItem = bar;
    bar.tintColor = [UIColor blackColor];
    
    
    

}
- (void)bar:(UIBarButtonItem *)bar {
    
    ActivityModel *model = [ActivityModel activityModel];
    model = _arrModel[_indexPath.row];
    
     BOOL result = [[MovieDataBaseHandle movieDataBaseHandle] selectModel:model];
  
    if (result == 0) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否收藏" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alertaction = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [[MovieDataBaseHandle movieDataBaseHandle] insertModel:model];
        } ]  ;
        UIAlertAction *alertaction1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [alert addAction:alertaction];
        [alert addAction:alertaction1];
        [self presentViewController:alert animated:YES completion:nil];
    } else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消收藏" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alertaction1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [[MovieDataBaseHandle movieDataBaseHandle] deleteModel:model];
        }];
        UIAlertAction *alertact = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [alert addAction:alertact];
        [alert addAction:alertaction1];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    


}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
